#include "../../matrix/matrix.h"
#include "square_op.h"
#include "../constant.h"
#include "multiply_op.h"
#include "../../factory/factory.h"
#include "../../factory/constant_factory.h"
#include "../../factory/multiply_factory.h"

Square::Square(Node* node) : OpNode({node}){
}

vector<Node*> Square::getGradNodes(Node* grad){
    auto nodes = *getNodeIn();
    Factory* factory;
    Shape* shape = nodes[0] -> getOutShape();
    factory = new ConstantFactory(Matrix(shape -> rows, shape -> cols, 2));
    Node* twos = factory -> createNode();
    delete factory;
    factory = new MultiplyFactory(grad, twos);
    Node* multiply = factory -> createNode();
    delete factory;
    factory = new MultiplyFactory(multiply, nodes[0]);
    Node* ret_grad = factory -> createNode();
    delete factory;
    return {ret_grad};
}

void Square::compute(Context* context){
    auto nodes = *getNodeIn();
    Matrix mat = context -> getFlowedData(nodes[0]);
    context -> setFlowedData(this, mat * mat);
}
